package utils

import (
	"fmt"
	"gopkg.in/ini.v1"
)

var (
	AppMode       string
	HttpPort      string
	JwtKey        string
	JwtExpire     int
	Email         string
	EmailAuthCode string

	Db         string
	DbHost     string
	DbPort     string
	DbName     string
	DbUser     string
	DbPassword string

	AccessKey   string
	SecretKey   string
	Bucket      string
	QiNiuServer string

	Addr     string
	Password string
	RedisDB  int
)

func init() {
	file, err := ini.Load("conf/config.ini")
	if err != nil {
		fmt.Println("config read error:", err)
	}

	LoadServer(file)
	LoadData(file)
	LoadQiNiu(file)
	LoadRedis(file)
}

func LoadServer(file *ini.File) {
	AppMode = file.Section("server").Key("AppMode").MustString("debug")
	HttpPort = file.Section("server").Key("HttpPort").MustString(":3000")
	JwtKey = file.Section("server").Key("JwtKey").MustString("xiaolatiao")
	JwtExpire = file.Section("server").Key("JwtExpire").MustInt(24)
	EmailAuthCode = file.Section("server").Key("EmailAuthCode").MustString("")
	Email = file.Section("server").Key("Email").MustString("1628652790@qq.com")
}

func LoadData(file *ini.File) {
	Db = file.Section("database").Key("Db").MustString("mysql")
	DbHost = file.Section("database").Key("DbHost").MustString("localhost")
	DbPort = file.Section("database").Key("DbPort").MustString("3306")
	DbName = file.Section("database").Key("DbName").MustString("gin-project")
	DbUser = file.Section("database").Key("DbUser").MustString("root")
	DbPassword = file.Section("database").Key("DbPassword").MustString("123456")
}

func LoadQiNiu(file *ini.File) {
	AccessKey = file.Section("qiniu").Key("AccessKey").MustString("")
	SecretKey = file.Section("qiniu").Key("SecretKey").MustString("")
	Bucket = file.Section("qiniu").Key("Bucket").MustString("")
	QiNiuServer = file.Section("qiniu").Key("QiNiuServer").MustString("")
}

func LoadRedis(file *ini.File) {
	Addr = file.Section("redis").Key("Addr").MustString("")
	Password = file.Section("redis").Key("Password").MustString("")
	RedisDB = file.Section("redis").Key("RedisDB").MustInt(0)
}
